const { Op } = require("sequelize");
let friends = require("../model/friends")
class Friend {
    /* 添加友链 */
    async addfriend(data) {
        let { friend_name, friend_url, friend_corver, friend_sign, friend_note } = data
        console.log("----",{ friend_name, friend_url, friend_corver, friend_sign, friend_note });
        if (!friend_note) {
            friend_note = "这个人很懒，什么都没有留下"
        }
        let result = await friends.create({ friend_name, friend_url, friend_corver, friend_sign, friend_note, friend_status: 2 })
        if (result) {
            return {
                code: 1,
                msg: "添加成功"
            }
        } else {
            return {
                code: 0,
                msg: "添加失败"
            }
        }
    }
    /* 获取友链 */
    async getfriends(data) {
        let { page, pageSize, friend_status } = data
        console.log({ page, pageSize, friend_status });
        friend_status = (typeof friend_status == "string") ? parseInt(friend_status) : friend_status;
        page = (typeof page == "string") ? parseInt(page) : page;
        pageSize = (typeof pageSize == "string") ? parseInt(pageSize) : pageSize;
        let result = await friends.findAll({
            limit: pageSize,
            offset: (page - 1) * pageSize,
            where: {
                friend_status
            }
        })
        let res = result.map(item => item.dataValues)
        if (res.length > 0) {
            return {
                code: 1,
                msg: "获取成功",
                data: res
            }
        } else {
            return {
                code: 0,
                msg: "暂无数据"
            }
        }
    }
    /* 获取数量 */
    async getcount(friend_status) {
        let num = await friends.count({
            where: {
                friend_status
            }
        })
        if (num) {
            return {
                code: 1,
                msg: "获取成功",
                data: num
            }
        } else {
            return {
                code: 0,
                msg: "暂无数据",
                data: 0
            }
        }
    }
    /* 获取单个 */
    async getOnefriend(id) {
        let result = await friends.findOne({ where: { id } })
        if (result) {
            return {
                code: 1,
                msg: "获取成功",
                data: result.dataValues
            }
        } else {
            return {
                code: 0,
                msg: "暂无数据"
            }
        }
    }
    /* 审核 */
    async auditFriend(id, fleg) {
        let result
        if (fleg == 1) {
            result = await friends.update({ friend_status: 1 }, { where: { id } })
        } else if (fleg == 2) {
            result = await friends.destroy({ where: { id } })
        }
        if (result) {
            return {
                code: 1,
                msg: "操作成功"
            }
        } else {
            return {
                code: 2,
                msg: "操作失败"
            }
        }
    }
    /* 搜索 */
    async searchFriend(keyword, page, pageSize, fleg) {
        page = (typeof page == "string") ? parseInt(page) : page;
        pageSize = (typeof pageSize == "string") ? parseInt(pageSize) : pageSize;
        let res = await friends.findAll({
            limit: pageSize,
            offset: pageSize * (page - 1),
            where: {
                friend_name: {
                    [Op.like]: `%${keyword}%`,
                },
                friend_status: fleg
            }
        })
        let ress = await friends.count({
            where: {
                friend_name: {
                    [Op.like]: `%${keyword}%`,
                },
                friend_status: fleg
            }
        })
        let data = res.map(item => item.dataValues)
        if (data.length > 0) {
            return {
                code: 1,
                msg: "获取成功",
                data: {
                    list: data,
                    total: ress
                }
            }
        } else {
            return {
                code: 0,
                msg: "没有更多数据",
                data: {
                    list: {},
                    total: ress
                }
            }

        }
    }
    /* 删除 */
    async deletee(idlist) {
        let res = await friends.destroy({
            where: {
                id: {
                    [Op.in]: idlist
                }
            }
        })
        if (res > 0) {
            return {
                code: 1,
                msg: "删除成功"
            }
        }else{
            return {
                code: 0,
                msg: "删除失败"
            }
        }
    }
    /* 修改 */
    async updatefriend(id,data){
        let res = await friends.update(data,{
            where:{
                id:id
            }
        })
        if(res[0]>0){
            return {
                code:1,
                msg:"修改成功"
            }
        }else{
            return {
                code:0,
                msg:"修改失败"
            }
        }
    }
}


module.exports = new Friend();